export function request(url, data = {}, method = 'GET') {
	let token = wx.getStorageSync('token');
	!token && (token = '');
	let header = {};
	if (url != 'wx/auth/token') {
		header = {
			'X-Access-Token': token
		}
	}
	var flag = false;
	setTimeout(() => {
		if (!flag) {
			uni.showLoading({
				title: '数据请求中'
			});
		}
	}, 2000)
	if (method == 'delete') {
		let query = ''
		for (var key in data) {
			query = key + '=' + data[key] + '&'
		}
		url = url + '?' + query;
		data = {}
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: getApp().globalData.BASE_URL + url,
			method,
			data,
			header,
			success(res) {
				flag = true;
				if (res.data.code == 401) {
					uni.removeStorageSync('token');
					uni.navigateTo({
						url: '/pages/login/login'
					})
				}
				if (res.data.code == 500) {
					if (uni.getStorageSync('token')&& res.data.message.indexOf("Token失效")!=-1) {
						uni.showToast({
							icon:'none',
							title:'登录失效，请重新登录'
						})
						uni.removeStorageSync('token');
						uni.navigateTo({
							url: '/pages/login/login'
						})
					}
				}
				uni.hideLoading();
				resolve(res);
			},
			fail(error) {
				flag = true;
				console.log('error', error)
				uni.showToast({
					title: '请求失败',
					icon: 'error'
				})
				reject(error);
			}
		})
	})
}
